import subprocess
import os

# 新建一个滑动操作命令
import config


def getSwipeCmd(start_x_location, start_y_location, end_x_location, end_y_location, time):
    return config.CMD_SWIPE + str(start_x_location) + ' ' + str(start_y_location) + ' ' + str(
        end_x_location) + ' ' + str(
        end_y_location) + ' ' + str(time)


# 默认滑动时间
def getSwipeCmdDefTime(start_x_location, start_y_location, end_x_location, end_y_location):
    return config.CMD_SWIPE + ' ' + str(start_x_location) + ' ' + str(start_y_location) + ' ' + str(
        end_x_location) + ' ' + str(end_y_location) + ' 100'


# 新建一个滑动操作对象
def getClickCmd(x, y):
    return config.CMD_TAP + ' ' + str(x) + ' ' + str(y)


def doSwipeDefTime(start_x_location, start_y_location, end_x_location, end_y_location):
    cmd = getSwipeCmdDefTime(start_x_location, start_y_location, end_x_location, end_y_location)
    return doCmd(cmd)


def doSwipe(start_x_location, start_y_location, end_x_location, end_y_location, time):
    cmd = getSwipeCmd(start_x_location, start_y_location, end_x_location, end_y_location, time)
    return doCmd(cmd)


# 向左翻页
def doTurnThePageLeft():
    return doSwipeDefTime('250', '1700', '600', '1700')


# 向右翻页
def doTurnThePageRight():
    return doSwipeDefTime('600', '1700', '250', '1700')


# 向上翻页
def doTurnThePageUp():
    print('向上滑动')
    return doSwipe('550', '700', '550', '1300', 200)


# 向下翻页
def doTurnThePageDown():
    print('向下滑动')
    return doSwipe('550', '1300', '550', '700', 200)


def doClick(x_location, y_location):
    cmd = getClickCmd(x_location, y_location)
    return doCmd(cmd)


# 唤醒屏幕
def wakeUp():
    return doCmd(config.CMD_WAKE_UP)


# 主页按钮
def home():
    return doCmd(config.CMD_HOME)


# 截图
def doScreenshots():
    if os.path.exists(config.CACHE_FILE_PATH_screencap):  # 如果文件存在
        # 删除文件，可使用以下两种方法。
        os.remove(config.CACHE_FILE_PATH_screencap)
        # os.unlink(cachePath)
    cmd = 'adb shell screencap -p > ' + config.CACHE_FILE_PATH_screencap
    doCmd(cmd)
    # 符号转义
    with open(config.CACHE_FILE_PATH_screencap, "rb") as f:
        bys = f.read()
        bys_ = bys.replace(b"\r\n", b"\n")  # 二进制流中的"\r\n" 替换为"\n"
    with open(config.CACHE_FILE_PATH_screencap, "wb") as f:
        f.write(bys_)
    f.close()


# 设置屏幕亮度
def setScreenBrightness(value):
    print('设置屏幕亮度至：' + str(value))
    cmd = 'adb shell settings put system screen_brightness ' + str(value)
    return doCmd(cmd)


# 关闭屏幕自动亮度
def closeScreenBrightnessAuto():
    print('关闭屏幕自动亮度')
    cmd = 'adb shell settings put system screen_brightness_mode 0'
    return doCmd(cmd)


# 开启屏幕自动亮度
def openScreenBrightnessAuto():
    print('开启屏幕自动亮度')
    cmd = 'adb shell settings put system screen_brightness_mode 1'
    return doCmd(cmd)


# 执行命令
def doCmd(cmd):
    # 返回结果
    # print("doCmd: " + str(cmd))
    return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()



if __name__ == '__main__':
    doScreenshots()
